<?php
require 'config.php';

/* ===== 搜索 & 分页 ===== */
$keyword = $_GET['kw'] ?? '';
$page    = max(1, intval($_GET['page'] ?? 1));
$limit   = 10;
$offset  = ($page - 1) * $limit;

/* 条件 */
$where = $keyword
  ? "WHERE title LIKE :kw OR content LIKE :kw"
  : '';
$params = $keyword ? [':kw'=>"%$keyword%"] : [];

/* 总数 */
$cntSql = "SELECT COUNT(*) FROM notes $where";
$cntStmt = $pdo->prepare($cntSql);
$cntStmt->execute($params);
$total = $cntStmt->fetchColumn();
$pages = max(1, ceil($total / $limit));

/* 列表 */
$sql = "SELECT * FROM notes $where ORDER BY id DESC LIMIT :offset,:limit";
$stmt = $pdo->prepare($sql);
foreach ($params as $k=>$v) $stmt->bindValue($k,$v);
$stmt->bindValue(':offset',$offset,PDO::PARAM_INT);
$stmt->bindValue(':limit',$limit,PDO::PARAM_INT);
$stmt->execute();
$notes = $stmt->fetchAll();
?>
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>开源粘贴板</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="assets/ztb.png">
  <link rel="stylesheet" href="assets/bootstrap.min.css">
  <link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="container-fluid">
  <div class="row">
    <!-- ===== 侧边栏 ===== -->
    <nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
      <div class="text-center py-3">
        <h5 class="text-white">开源粘贴板</h5>
      </div>
      <ul class="nav flex-column px-2">
        <li class="nav-item">
          <a class="nav-link active" href="index.php">📋 全部记录</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="add.php">✍️ 写新记录</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#about" data-bs-toggle="modal">ℹ️ 关于</a>
        </li>
      </ul>
    </nav>

    <!-- ===== 主内容 ===== -->
    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main">
      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">粘贴板列表</h1>
        <a href="add.php" class="btn btn-primary btn-sm">✍️ 写新记录</a>
      </div>

      <!-- 搜索 -->
      <form method="get" class="row g-2 mb-3">
        <div class="col-auto">
          <input type="text" class="form-control" name="kw" placeholder="搜索标题/内容" value="<?=htmlspecialchars($keyword)?>">
        </div>
        <div class="col-auto">
          <button class="btn btn-outline-secondary">搜索</button>
        </div>
      </form>

      <!-- 记录卡片 -->
      <?php if (!$notes): ?>
        <div class="alert alert-info">暂无记录，快来发表第一条吧！</div>
      <?php else: ?>
        <?php foreach ($notes as $n): ?>
          <div class="card mb-3">
            <div class="card-header d-flex justify-content-between">
              <strong><?=htmlspecialchars($n['title'])?></strong>
              <small class="text-muted"><?=date('Y-m-d H:i',strtotime($n['created_at']))?></small>
            </div>
            <div class="card-body">
              <p class="mb-2"><?=nl2br(htmlspecialchars($n['content']))?></p>
              <div class="d-flex align-items-center">
                <small class="me-auto">— <?=htmlspecialchars($n['author'])?></small>
                <a href="edit.php?id=<?=$n['id']?>" class="btn btn-sm btn-outline-primary me-2">编辑</a>
                <a href="delete.php?id=<?=$n['id']?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除?')">删除</a>
              </div>
            </div>
          </div>
        <?php endforeach; ?>

        <!-- ===== 分页 ===== -->
        <?php if ($pages>1): ?>
        <nav>
          <ul class="pagination justify-content-center">
            <?php for($i=1;$i<=$pages;$i++): ?>
              <li class="page-item <?=($i==$page?'active':'')?>">
                <a class="page-link" href="?page=<?=$i?>&kw=<?=urlencode($keyword)?>"><?=$i?></a>
              </li>
            <?php endfor; ?>
          </ul>
        </nav>
        <?php endif; ?>
      <?php endif; ?>
    </main>
  </div>
</div>

<!-- 关于模态框 -->
<div class="modal fade" id="about" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">关于</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        开源粘贴板 <br>
        基于 PHP + MySQL,Bootstrap 5 美化<br>
        MIT 许可证，可自由二次开发。
      </div>
    </div>
  </div>
</div>

<script src="assets/bootstrap.bundle.min.js"></script>
</body>
</html>